wx.config({
    debug: false,
    appId: appId,
    timestamp: timestamp,
    nonceStr: nonceStr,
    signature: signature,
    jsApiList: [
		//所有要调用的 API 都要加到这个列表中
		'checkJsApi',
		'onMenuShareTimeline',
		'onMenuShareAppMessage',
		'onMenuShareQQ',
		'onMenuShareWeibo',
		'hideMenuItems',
		'showMenuItems',
		'hideAllNonBaseMenuItem',
		'showAllNonBaseMenuItem',
		'translateVoice',
		'startRecord',
		'stopRecord',
		'onRecordEnd',
		'playVoice',
		'pauseVoice',
		'stopVoice',
		'uploadVoice',
		'downloadVoice',
		'chooseImage',
		'previewImage',
		'uploadImage',
		'downloadImage',
		'getNetworkType',
		'openLocation',
		'getLocation',
		'hideOptionMenu',
		'showOptionMenu',
		'closeWindow',
		'scanQRCode',
		'chooseWXPay',
		'openProductSpecificView',
		'addCard',
		'chooseCard',
		'openCard',
		'error'
    ]
});

function getLocationByWeixin() {
	var $move = localStorage.getItem('move');
	if (typeof showLocationInfo != 'function') {
		return false;
	}
	
	if ($move !== null) {
		var res = $.parseJSON(localStorage.getItem('cache'));
		try {
			if (!$.isEmptyObject(res)) {
				var time = new Date().getTime();
				if (time - res.time > 1800000) {
					localStorage.removeItem('cache');
					localStorage.removeItem('move');
					getLocationByWeixin();
					return false;
				}
				
	        	var gc = new BMap.Geocoder();
	        	var point = new BMap.Point(res.baidux, res.baiduy); //初始化地图中心点
	        	gc.getLocation(point, function(rs){
	        		showLocationInfo(point, rs);
	        	});
			} else {
				localStorage.removeItem('cache');
				localStorage.removeItem('move');
				getLocationByWeixin();
			}
			
        } catch (e) {
        	alert(e);
        }
	} else {
		wx.getLocation({
		    type: 'wgs84', // 默认为wgs84的gps坐标，如果要返回直接给openLocation用的火星坐标，可传入'gcj02'
		    success: function (res) {
		        var latitude = res.latitude; // 纬度，浮点数，范围为90 ~ -90
		        var longitude = res.longitude; // 经度，浮点数，范围为180 ~ -180。
		        var speed = res.speed; // 速度，以米/每秒计
		        var accuracy = res.accuracy; // 位置精度
		        
		        res.time = new Date().getTime();
		        if (speed == 0) {
		        	localStorage.setItem('move', true);
		        }
		        
		        try {
		        	var gc = new BMap.Geocoder();
		        	$.ajax({
		        		url: 'http://www.joyweixin.com/clients/mdd/index.php/Index/gpsChange',
		        		data: {
		        			lngAndLat :res.longitude +',' +res.latitude
		        		},
		        		dataType: 'json',
		        		success: function (result) {
		        			res.baidux = result.data.x;
		        			res.baiduy = result.data.y;
		        			
		        			localStorage.setItem('cache', JSON.stringify(res));
		        			localStorage.setItem('location', result.data.y +',' +result.data.x);
		    	        	var point = new BMap.Point(result.data.x, result.data.y); //初始化地图中心点
		    	        	gc.getLocation(point, function(rs){
		    	        		showLocationInfo(point, rs);
		    	        	});
		        		}
		        	});
		        	
		        } catch (e) {
		        	alert(e);
		        }
		    }
		});
	}
}

wx.ready(function () {
  // 在这里调用 API
//	wx.hideOptionMenu();
//	wxEventBind();
	
	if(typeof startGetLocation == 'undefined' || startGetLocation) {
		getLocationByWeixin();
	}
});